web/src/pages/[category].astro
111 lines · 2651 bytes
1---
2import Layout from '@layouts/Layout.astro';
3import SectionList from '@components/things/SectionList.astro';
4import Main from '@components/scafold/MainCard.astro';
5
6import { fetchData, slugify } from '@utils/fetch-data';
7
8import type { Section } from '../types/Service';
9import Button from '@components/form/Button.astro';
10
11interface Props {
12 title: string;
13 sections: Section[];
14}
15
16const { title, sections } = Astro.props;
17
18const categories = (await fetchData()).categories;
19
20export async function getStaticPaths() {
21 const pages = await fetchData().then((data) => {
22 return data.categories.map((category) => {
23 return {
24 category: slugify(category.name),
25 title: category.name,
26 sections: category.sections,
27 };
28 });
29 });
30
31 return pages.map(({ category, title, sections }) => {
32 return {
33 params: { category },
34 props: { title, sections },
35 };
36 });
37}
38
39const makeCarasolData = () => {
40 return {
41 '@context': 'https://schema.org',
42 '@type': 'ItemList',
43 itemListElement: [
44 sections.map((section, index) => ({
45 '@type': 'ListItem',
46 position: index + 1,
47 url: `https://awesome-privacy.xyz/${slugify(title)}/${slugify(section.name)}`,
48 item: {
49 '@type': 'Service',
50 name: section.name,
51 url: `https://awesome-privacy.xyz/${slugify(title)}/${slugify(section.name)}`,
52 },
53 })),
54 ],
55 };
56};
57
58const makeBreadcrumbs = () => {
59 return [
60 { name: 'Home', item: '/' },
61 { name: title, item: slugify(title) },
62 ];
63};
64
65const makePaginationLinks = () => {
66 const index = categories.findIndex((category) => category.name === title);
67 const previousCategory = index > 0 ? categories[index - 1].name : null;
68 const nextCategory =
69 index < categories.length - 1 ? categories[index + 1].name : null;
70 return { previous: previousCategory, next: nextCategory };
71};
72
73const { previous, next } = makePaginationLinks();
74---
75
76<Layout
77 title={`${title} | Awesome Privacy`}
78 breadcrumbs={makeBreadcrumbs()}
79 customSchemaJson={makeCarasolData()}
80>
81 <Main>
82 <div class="breadcrumbs">
83 <span>
84 <a href="/">Awesome Privacy</a>
85 ➔ <a href={`/${slugify(title)}`}>{title}</a>
86 </span>
87 </div>
88 <SectionList title={title} sections={sections} bigTitle={true} />
89 Or, <a href={`/all#${slugify(title)}`}>Browse All {title}</a>
90 <div class="pagination-navigation">
91 {
92 previous ? (
93 <Button url={`/${slugify(previous)}`}>
94 <span>← Previous</span>
95 <p>{previous}</p>
96 </Button>
97 ) : (
98 <p class="nothing" />
99 )
100 }
101 {
102 next && (
103 <Button url={`/${slugify(next)}`}>
104 <span>Next →</span>
105 <p>{next}</p>
106 </Button>
107 )
108 }
109 </div>
110 </Main>
111</Layout>